﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cn.jpush.api.common;
using cn.jpush.api.common.resp;
using cn.jpush.api.device;
using cn.jpush.api.push.mode;
using cn.jpush.api.push.notification;

namespace cn.jpush.api.myexample
{
    class Program
    {
        public static string app_key = "3b560d1a1d10218c695e7616";
        public static string master_secret = "0d356624961371b42c925bc5";
        static void Main(string[] args)
        {
            Console.WriteLine("*****开始发送******");
            JPushClient client = new JPushClient(app_key, master_secret);

            PushPayload payload = PushObject();
            try
            {
                var result = client.SendPush(payload);

                System.Threading.Thread.Sleep(10000);
                //如需查询上次推送结果执行下面的代码
                Console.WriteLine(result);
                Console.ReadLine();
            }
            catch (APIRequestException e)
            {
                Console.WriteLine("Error response from JPush server. Should review and fix it. ");
                Console.WriteLine("HTTP Status: " + e.Status);
                Console.WriteLine("Error Code: " + e.ErrorCode);
                Console.WriteLine("Error Message: " + e.ErrorMessage);
            }
            catch (APIConnectionException e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public static PushPayload PushObject()
        {
            PushPayload pushPayload = new PushPayload();
            pushPayload.platform = Platform.android();
            var audience = Audience.s_registrationId("170976fa8a8c0957c65", "1104a89792a5c864e6b");//指定ID
            //var audience = Audience.all();//指定所有
            pushPayload.audience = audience;
            var notification = new Notification().setAlert("alert content");
            notification.AndroidNotification = new AndroidNotification().setTitle("Android Title");
            notification.AndroidNotification.AddExtra("extra_key", "extra_value");
            pushPayload.notification = notification.Check();
            return pushPayload;
        }
    }
}
